package com.cec.service.admin.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.context.BaseContext;
import com.cec.exception.OperationDataException;
import com.cec.mapper.admin.FeedbackMapper;
import com.cec.result.ResultCode;
import com.cec.service.admin.FeedbackService;
import com.cec.dto.FeedbackDTO;
import com.cec.entity.Feedback;
import com.cec.result.Result;
import com.cec.vo.FeedbackVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 反馈表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class FeedbackServiceImpl extends ServiceImpl<FeedbackMapper, Feedback> implements FeedbackService {

    @Override
    public Result pageQueryFeedbackList(FeedbackDTO feedbackDTO) {
        QueryWrapper<FeedbackDTO> wrapper = new QueryWrapper<>();
        if (feedbackDTO.getTypeId() != null)
            wrapper.eq("f.feedback_type_id", feedbackDTO.getTypeId());
        wrapper.eq("f.deleted",0).orderByDesc("f.create_time");
        IPage<FeedbackVO> feedbackVOIPage = baseMapper.pageQueryFeedbackList(new Page<>(feedbackDTO.getPage(), feedbackDTO.getPageSize()), wrapper);
        return Result.success(feedbackVOIPage.getRecords(), feedbackVOIPage.getTotal());
    }

    @Override
    public Result logicDeleteFeedbackById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result logicBatchDeleteFeedbackByIds(List<Integer> ids) {
        int i = baseMapper.deleteBatchIds(ids);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }

    @Override
    public Result addFeedback(FeedbackDTO feedbackDTO) {
        Feedback feedback = new Feedback();
        BeanUtils.copyProperties(feedbackDTO, feedback);
        feedback.setUserId(BaseContext.getCurrentId());
        int i = baseMapper.insert(feedback);
        if (i>0)
            return Result.success();
        throw new OperationDataException(ResultCode.OPERATION_DATA_ERROR);
    }
}
